package org.tea.inventory.admin.web.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.domain.tea.CustomerSmall;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.ProductCategory;
import org.tea.inventory.domain.tea.StoreInfo;
import org.tea.inventory.service.IStoreInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店信息 Controller
 * 
 * @author dongxb
 * @date 2025-04-14
 */
@Api(tags = "基础设置-门店信息控制器")
@Slf4j
@RestController
@RequestMapping("/admin/store-info")
@RequiredArgsConstructor
public class StoreInfoController extends BaseController
{
    @Autowired
    private IStoreInfoService storeInfoService;

    /**
     * 查询门店信息列表
     */
    @GetMapping("page")
    @ApiOperation(value = "获取门店信息信息列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(StoreInfo storeInfo)
    {
        startPage();
        List<StoreInfo> list = storeInfoService.selectStoreInfoList(storeInfo);
        return getDataTable(list);
    }

    /**
     * 导出门店信息列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出门店信息列表")
    public void export(StoreInfo storeInfo, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "门店信息列表";
        String header = "门店编号,门店名称,联系人,联系人联系电话,门店地址,创建时间";
        String column = "storeCode,storeName,contactsId,contactsPhone,storeAddress,createDateStr";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<StoreInfo> list = storeInfoService.selectStoreInfoList(storeInfo);
            List<Map> listMap = JSONArray.parseArray(JSON.toJSONString(list), Map.class);
            ExportExcel.export(request, response, listMap, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取门店信息详细列表(不分页)
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取门店信息详细信息(不分页)")
    @ApiResponses(
            @ApiResponse(code = 0 , message = "成功" , response = ProductCategory.class)
    )
    public BaseResponse list(StoreInfo storeInfo) {
        List<StoreInfo> storeInfos = storeInfoService.selectStoreInfoList(storeInfo);
        return ResponseUtil.ok(storeInfos);
    }

    /**
     * 新增门店信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增门店信息")
    public BaseResponse add(@RequestBody StoreInfo storeInfo)
    {
        return ResponseUtil.ok(storeInfoService.insertStoreInfo(storeInfo));
    }

    /**
     * 修改门店信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改门店信息")
    public BaseResponse edit(@RequestBody StoreInfo storeInfo)
    {
        return ResponseUtil.ok(storeInfoService.updateStoreInfo(storeInfo));
    }
    
    /**
     * 删除门店信息
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除门店信息")
    public BaseResponse remove(Long id)
    {
        return ResponseUtil.ok(storeInfoService.deleteStoreInfoById(id));
    }

}
